package com.gwd.task.model;

import lombok.Data;

import javax.validation.constraints.NotNull;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.List;

/**
 * @author zhiyongming
 * @date 2020/11/9 13:43
 */
@Data
public class PageUtils {
    @NotNull(message = "当前页不能为空")
    private int pageNum; // 当前页
    @NotNull(message = "每页大小不能为空")
    private int pageSize; // 每页大小
    private String sortColumn; // 排序列
    private String sortType; // 排序类型 asc desc

    private int totalNum; // 总记录数
    private int totalPage; // 总页码数
    private List<?> content; // 当前页数据

    public PageUtils convert(Page<?> page) {
//        this.setContent(page.getContent());
        this.setTotalNum((int)page.getTotalElements());
        this.setTotalPage(page.getTotalPages());
        return this;
    }

    public Pageable convert() {
        return PageRequest.of(this.getPageNum()-1, this.getPageSize());
    }

    public Pageable convert(String sortColumn, String sortType) {
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, sortColumn);
        return PageRequest.of(this.getPageNum()-1, this.getPageSize(), Sort.by(order));
    }
}
